package com.west_traveler.ximalayafm;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.test.mock.MockApplication;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.west_traveler.application.BaseApplication;
import com.west_traveler.entity.entity_anchor.EntityAnchor;
import com.west_traveler.entity.entity_anchor.Item_list_Anchor;
import com.west_traveler.entity.entity_live_page.entity_live_second_page.RadioInfo;
import com.west_traveler.entity.entity_live_page.entity_live_second_page.SecondFirstData;
import com.west_traveler.inteface.LiveHttpService;
import com.west_traveler.recyclerview_adapter.NormalLiveSecondRecyclerAdapter;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class NormalLiveSecondPageActivity extends AppCompatActivity implements NormalLiveSecondRecyclerAdapter.OnNormalLiveClickListener {
    private int localPage=1;
    private int countryPage=1;
    private int netPage=1;
    private String tag;
    private boolean loading;
    private LiveHttpService service;
    private NormalLiveSecondRecyclerAdapter adapter;
    private RecyclerView recycler;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_normal_live_second_page);
        //获取传递过来的标题并展示
        setPageTitle();
        //设置左上角返回键
        setBack();
        //关于recyclerView
        aboutRecyclerView();

    }


    //关于recyclerView
    private void aboutRecyclerView() {
        recycler = (RecyclerView) findViewById(R.id.recycler_normal_live_second_id);
        service = ((BaseApplication) getApplication())
                .getLiveHttpService();
        Call<SecondFirstData> call=null;
        switch (tag) {
            case "localRadio":
                call= service.getSecondLocalData(localPage, 20, 110000, 2);
                break;
            case "countryRadio":
                call= service.getSecondCountryData(countryPage, 20, 1);
                break;
            case "internetRadio":
                call= service.getSecondNetData(netPage, 20, 3);
                break;
        }
        call.enqueue(new Callback<SecondFirstData>() {
            @Override
            public void onResponse(Call<SecondFirstData> call, Response<SecondFirstData> response) {
                List<RadioInfo> datas = response.body().getResult();
                adapter = new NormalLiveSecondRecyclerAdapter(NormalLiveSecondPageActivity.this);
                adapter.addDatas(datas);
                recycler.setAdapter(adapter);
                //recycler设置点击监听
                adapter.setListener(NormalLiveSecondPageActivity.this);
                intent = new Intent(NormalLiveSecondPageActivity.this,PlayActivity.class);

            }

            @Override
            public void onFailure(Call<SecondFirstData> call, Throwable t) {

            }
        });

        recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
               // layoutManager.getItemCount() 可以得到当前RecyclerView中Item的总数目
//                layoutManager.findLastVisibleItemPosition() 得到最后一个可见的Item的位置position
                //    Log.i("100","进入滑动判断");
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int totalItemCount = layoutManager.getItemCount();
                int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
                //   Log.i("100","itemtotal="+totalItemCount);
                //   Log.i("100","lastVisibleItem="+lastVisibleItem);
                if(!loading && totalItemCount==lastVisibleItem+1){
                    //    Log.i("100","进入条目判断.........................................");
                    loading=true;
                    Call<SecondFirstData> call=null;
                    switch (tag) {
                        case "localRadio":
                            call= service.getSecondLocalData(++localPage, 20, 110000, 2);
                            call.enqueue(new Callback<SecondFirstData>() {
                                @Override
                                public void onResponse(Call<SecondFirstData> call, Response<SecondFirstData> response) {
                                    List<RadioInfo> datas = response.body().getResult();
                                    adapter.addDatas(datas);
                                    loading=false;
                                    if (localPage==2){
                                        loading=true;
                                    }
                                }

                                @Override
                                public void onFailure(Call<SecondFirstData> call, Throwable t) {

                                }
                            });
                            break;
                        case "countryRadio":
                            call= service.getSecondCountryData(++countryPage, 20, 1);
                            call.enqueue(new Callback<SecondFirstData>() {
                                @Override
                                public void onResponse(Call<SecondFirstData> call, Response<SecondFirstData> response) {
                                    List<RadioInfo> datas = response.body().getResult();
                                    adapter.addDatas(datas);
                                    loading=false;
                                    if (countryPage==2){
                                        loading=true;
                                    }
                                }

                                @Override
                                public void onFailure(Call<SecondFirstData> call, Throwable t) {

                                }
                            });
                            break;
                        case "internetRadio":
                            call= service.getSecondNetData(++netPage, 20, 3);
                            call.enqueue(new Callback<SecondFirstData>() {
                                @Override
                                public void onResponse(Call<SecondFirstData> call, Response<SecondFirstData> response) {
                                    List<RadioInfo> datas = response.body().getResult();
                                    adapter.addDatas(datas);
                                    loading=false;
                                    if (netPage==4){
                                        loading=true;
                                    }
                                }

                                @Override
                                public void onFailure(Call<SecondFirstData> call, Throwable t) {

                                }
                            });
                            break;
                    }

                }

            }
        });



    }

    //设置左上角返回键
    private void setBack() {
        ImageView ivBack = (ImageView) findViewById(R.id.iv_radio_back);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                overridePendingTransition(android.R.anim.fade_in, R.anim.push_left_out);
            }
        });
    }

    //获取传递过来的标题并展示
    private void setPageTitle() {
        Intent intent = getIntent();
        String titleStr = intent.getStringExtra("title");
        tag = intent.getStringExtra("tag");
        TextView tvTitle = (TextView) findViewById(R.id.tv_live_second_page);
        tvTitle.setText(titleStr);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(android.R.anim.fade_in, R.anim.push_left_out);
    }

    //适配器中自定义接口实现类
    @Override
    public void onNormalLiveClick(int position, long id, RadioInfo radioInfo) {
        intent.putExtra("title", radioInfo.getProgramName());
        intent.putExtra("playUrl32", radioInfo.getRadioPlayUrl().getRadio_24_ts());
        intent.putExtra("coverLarge", radioInfo.getRadioCoverLarge());
        intent.putExtra("nickname", radioInfo.getRname());
        intent.putExtra("smallLogo", radioInfo.getRadioCoverSmall());
        intent.putExtra("startService",true);
        startActivity(intent);
        overridePendingTransition(R.anim.down_in,android.R.anim.fade_out);
    }
}
